package lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class Test {

    //无返回值无参数
    @FunctionalInterface
    interface NoParameterNoReturn {
        void test();
    }
    //无返回值一个参数
    @FunctionalInterface
    interface OneParameterNoReturn {
        void test(int a);
    }
    //无返回值多个参数
    @FunctionalInterface
    interface MoreParameterNoReturn {
        void test(int a,int b);
    }
    //有返回值无参数
    @FunctionalInterface
    interface NoParameterReturn {
        int test();
    }
    //有返回值一个参数
    @FunctionalInterface
    interface OneParameterReturn {
        int test(int a);
    }
    //有返回值多参数
    @FunctionalInterface
    interface MoreParameterReturn {
        int test(int a,int b);
    }

    public static void main(String[] args) {
        int c = 100;
        MoreParameterReturn moreParameterReturn = new MoreParameterReturn() {
            @Override
           public int test(int a,int b) {
                System.out.println("c="+c);
                return a-b;
            }
        };
        System.out.println(moreParameterReturn.test(30, 20));
    }

    public static void mai1n(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "lambda");
        map.put(3, "hi");
        map.put(4, "lambda");
        map.forEach(new BiConsumer<Integer, String>(){
            @Override
            public void accept(Integer k, String v){
                System.out.println(k + "=" + v);
            }
        });
        System.out.println("=============");
        map.forEach((k,v)-> System.out.println(k + "=" + v));
    }
    public static void mai2n(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Hello");
        list.add("lambda");
        list.add("hi");
        list.add("lambda");
        //常规写法
        list.forEach(new Consumer<String>(){
            @Override
            public void accept(String str){
                //简单遍历集合中的元素。
                System.out.println(str+" ");
            }
        });
        System.out.println("============");
        //lambda
        list.forEach(s -> {
            System.out.println(s);
        });
    }
    public static void main12(String[] args) {
        //常规方法
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        //lambda
        PriorityQueue<Integer> priorityQueue1 = new PriorityQueue<>((o1, o2) ->  o1-o2);
    }
    public static void main1(String[] args) {
        //有返回值多参数
        MoreParameterReturn moreParameterReturn = (a,b)->{
            return a*b;
        };
        System.out.println(moreParameterReturn.test(10, 20));
        System.out.println("============");
        //无返回值多个参数
        MoreParameterNoReturn moreParameterNoReturn = (a,b) ->{
            System.out.println(a+b);
        };
        moreParameterNoReturn.test(10,20);
    }
}
